package com.hlf.eduservice.controller;


import com.atguigu.commonutils.R;
import com.hlf.eduservice.entity.EduChapter;
import com.hlf.eduservice.entity.chapter.ChapterVo;
import com.hlf.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author hlf
 * @since 2022-11-27
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    // 课程大纲列表
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo (@PathVariable String courseId){
     List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return  R.ok().data("allChapterVideo",list);
    }

    // 添加章节
    @PostMapping("addChapter/{courseId}")
    public R addChapter(@RequestBody EduChapter eduChapter,@PathVariable String courseId ){
        eduChapter.setCourseId(courseId);
        chapterService.save(eduChapter);
        return R.ok();
    }

    // 根据章节id查询
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", chapter);
    }

    // 修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    // 删除
    @DeleteMapping("{chapterId}")
    public R removeById(@PathVariable String chapterId){
        boolean result = chapterService.deleteChapter(chapterId);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }

}

